package com.songmao.cleartransaction.util;

import com.zf.zson.ZSON;
import com.zf.zson.exception.ZsonException;
import com.zf.zson.result.ZsonResult;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Remy
 * @description : 解析JSON
 * @date 2020-04-03 17:27
 */

@Component
public class ParsingJson {


    /**
     * @description: 给定一个json，在给定一个路径；则返回对应的value;没有则返回null
     * @Param path, json
     * @return: Object
     * @author: Remy
     * @time: 2020/4/3 17:35
     */
    public static Object getPathCode(String json, String path) {
        if (isPath(json, path)) {
            ZsonResult zr = ZSON.parseJson(json);
            return zr.getValue(path);
        } else {
            return null;
        }

    }

    /**
     * @param json,path
     * @description: 给定一个json，在给一个路径;返回所有的值; 没有则抛出异常
     * @return: List<Object>
     * @author: Remy
     * @time: 2020/4/3 17:36
     */
    public static List<Object> getPathCodes(String json, String path) {
        try {
            ZsonResult zr = ZSON.parseJson(json);
            if (isPath(json, path)) {
                return zr.getValues(path);
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }


    /**
     * @param json,path
     * @description: 判断json路径是否存在
     * @return: boolean
     * @author: Remy
     * @time: 2020/4/3 17:29
     */
    private static boolean isPath(String json, String path) {
        try {
            ZsonResult zr = ZSON.parseJson(json);
            List<String> paths = zr.getPaths();
            for (String p : paths) {
                if (p.equals(path) || p.contains(path)) {
                    return true;
                }
            }
        } catch (ZsonException e) {
            return false;
        }
        return false;
    }

    public static List<String> getPaths(String json) {
        try {
            ZsonResult zr = ZSON.parseJson(json);
            return zr.getPaths();
        } catch (ZsonException e) {
            return null;
        }

    }

    public static void update(String json, String key, Object vaue) {
        ZsonResult zr = ZSON.parseJson(json);
        zr.updateValue(key, vaue);

    }

    public static boolean judgmentJson(String json) {
        try {
            ZsonResult zr = ZSON.parseJson(json);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static void main(String[] args) {
        String json="[{\"Id\":\"12125467\",\"Price\":\"246.97\",\"Volume\":\"6.06\"},{\"Id\":\"12125469\",\"Price\":\"246.95\",\"Volume\":\"10.17\"},{\"Id\":\"12125471\",\"Price\":\"246.95\",\"Volume\":\"4.15\"},{\"Id\":\"12125473\",\"Price\":\"246.94\",\"Volume\":\"1.99\"},{\"Id\":\"12125475\",\"Price\":\"246.93\",\"Volume\":\"1\"}]";
        System.out.println(getPathCode(json,"/*[0]/Id"));
        System.out.println(getPaths(json));
        System.out.println(ZSON.parseJson(json).getResult());
        System.out.println("12125467".contains(json));

    }


}
